﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.Search;

namespace Slippor.Douban.Radio.BaseForm.CustomControls
{
    public partial class SearchToolStripMenuItem : ToolStripMenuItem, ISearchContainer
    {
        private readonly Dictionary<String, ToolStripMenuItem> _searchItems =  new Dictionary<string, ToolStripMenuItem>();
        private readonly Dictionary<String, ToolStripMenuItem> _searchAlbumItems = new Dictionary<String, ToolStripMenuItem>();

        private DoubanAccount _account { get { return ((DoubanForm)_mainApplication).CurrentAccount; } }

        private Song _searchSong;
        public Song SearchSong
        {
            get { return _searchSong; }
            set
            {
                _searchSong = value;
                if (_searchSong != null)
                {
                    toolStripTextBoxKeyword.Text = RadioUtility.VerifyText(_searchSong.Title);
                    if (_searchSong.Album != null)
                    {
                        _albumItem.Text = RadioUtility.VerifyText(String.Format("{1}：{0}", _searchSong.Album.Name, StringResource.Album));
                        SetItemsVisible(true);
                        if (_account.IsLoged)
                        {
                            _favItem.Text = RadioUtility.VerifyText(_searchSong.FaveState != FavState.Fav ? String.Format("{1} : {0}", _searchSong.Title, StringResource.Command_Like) : String.Format("{1}：{0}", _searchSong.Title, StringResource.Command_Unlike));
                            _favItem.Visible = true;
                        }
                        else
                        {
                            _favItem.Visible = false;
                        }
                    }
                    else
                    {
                        SetItemsVisible(false);
                        _favItem.Visible = false;
                    }
                }
                else
                {
                    SetItemsVisible(false);
                    _favItem.Visible = false;
                }
            }
        }

        private void SetItemsVisible(bool visible)
        {
            _albumItem.Visible = visible;
            _albumSeparator.Visible = visible;
            foreach (ToolStripMenuItem item in _searchAlbumItems.Values)
            {
                item.Visible = visible;
            }
        }

        public string KeyWord
        {
            get { return toolStripTextBoxKeyword.Text; }
            set
            {
                toolStripTextBoxKeyword.Text = value;
                _searchSong = null;
                _albumItem.Visible = false;
                _favItem.Visible = false;
                SetItemsVisible(false);
            }
        }

        private IApplication _mainApplication;
        public IApplication MainApplication
        {
            get
            {
                return _mainApplication;
            }
            set
            {
                _mainApplication = value;
            }
        }

        private void RefreshedFav(ref Song song)
        {
            _favItem.Text = RadioUtility.VerifyText(song.FaveState != FavState.Fav ? String.Format("{1} : {0}", _searchSong.Title, StringResource.Command_Like) : String.Format("{1}：{0}", _searchSong.Title, StringResource.Command_Unlike));
            Owner.Show();
            ShowDropDown();
        }

        private ToolStripSeparator _albumSeparator = new ToolStripSeparator();
        public SearchToolStripMenuItem()
        {
            InitializeComponent();

            _albumItem.Click += (sender, e) =>
                {
                    RadioUtility.Browse(String.Format("http://music.douban.com/subject/{0}/", _searchSong.Album.AlbumID));
                };
            _favItem.Click += (sender, e) =>
                                 {
                                     if(_searchSong != null)
                                     {
                                         ((DoubanForm)_mainApplication).MakeFav(_searchSong, RefreshedFav);
                                     }
                                 };

        }

        public void Add(ISearch search)
        {
            var searchItem = new ToolStripMenuItem(search.Name) { Tag = search, CheckOnClick = true, };
            searchItem.Click += searchItem_Click;
            DropDownItems.Add(searchItem);
            _searchItems.Add(search.Name, searchItem);
            if (_defaultSearchItem == null)
            {
                DefaultSearchItem = searchItem;
            }
            if (search is ISearchAlbum)
            {
                AddAlbum(search);
            }
        }

        public void AddAlbum(ISearch search)
        {
            var searchAlbumItem = new ToolStripMenuItem(search.Name) { Tag = search };
            searchAlbumItem.Click += searchAlbumItem_Click;
            DropDownItems.Insert(DropDownItems.IndexOf(_albumSeparator), searchAlbumItem);
            _searchAlbumItems.Add(search.Name, searchAlbumItem);
            if (_searchSong == null || _searchSong.Album == null)
            {
                searchAlbumItem.Visible = false;
            }
        }

        public void Remove(ISearch search)
        {
            if (_searchItems[search.Name] == _defaultSearchItem)
            {
                _defaultSearchItem = null;
            }
            DropDownItems.Remove(_searchItems[search.Name]);
            _searchItems.Remove(search.Name);
            if (search is ISearchAlbum)
            {
                DropDownItems.Remove(_searchAlbumItems[search.Name]);
                _searchAlbumItems.Remove(search.Name);
            }
        }

        private void searchItem_Click(object sender, EventArgs e)
        {
            var searchItem = sender as ToolStripMenuItem;
            if (searchItem != null)
            {
                var search = searchItem.Tag as ISearch;
                if (search != null)
                {
                    DefaultSearchItem = searchItem;
                    StaticResources.Settings.DefaultSearch = search.Name;
                    StaticResources.Settings.Save();
                    if (_searchSong != null && KeyWord == _searchSong.Title)
                    {
                        search.Search(_searchSong);
                    }
                    else
                    {
                        search.Search(KeyWord);
                    }
                }
            }
        }

        private void searchAlbumItem_Click(object sender, EventArgs e)
        {
            var searchItem = sender as ToolStripMenuItem;
            if (searchItem != null)
            {
                var search = searchItem.Tag as ISearchAlbum;
                if (search != null)
                {
                    if (_searchSong != null && _searchSong.Album != null)
                    {
                        search.Search(_searchSong.Album);
                    }
                }
            }
        }

        #region 默认搜索引擎
        private ToolStripMenuItem _defaultSearchItem;
        public ToolStripMenuItem DefaultSearchItem
        {
            set
            {
                if (_defaultSearchItem != null)
                {
                    _defaultSearchItem.Checked = false;
                }
                _defaultSearchItem = value;
                _defaultSearchItem.Checked = true;
            }
        }
        public ISearch DefaultSearch
        {
            get
            {
                if (_defaultSearchItem != null)
                {
                    return _defaultSearchItem.Tag as ISearch;
                }
                return null;
            }
        }

        public void SetDefault(String searchKey)
        {
            if (_searchItems.ContainsKey(searchKey))
            {
                DefaultSearchItem = _searchItems[searchKey];
            }
        }
        #endregion
    }
}